package cn.wolfcode.exception;

import cn.wolfcode.qo.JsonResult;
import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 对控制器进行增强处理
 */
@ControllerAdvice
public class CRMExceptionHandler {

    @ExceptionHandler(RuntimeException.class)
    public Object handleException(RuntimeException e, HandlerMethod method, HttpServletResponse response){
        e.printStackTrace();//打印异常信息方便开发找bug

        //判断是否带有ResponseBody注解
        if (method.hasMethodAnnotation(ResponseBody.class)){
            String errormsg = "操作失败,请联系管理员";
            //判断是否逻辑异常,是的话直接使用自己的逻辑异常
            if (e instanceof LogicException){
                errormsg = e.getMessage();
            }
            //带注解的话,返回jsonResult
            JsonResult jsonResult = new JsonResult(false,errormsg);
            //使用response对象输出数据(json字符串)
            response.setContentType("application/json;charset=utf-8");
            try {
                response.getWriter().print(JSON.toJSONString(jsonResult));
            } catch (IOException e1) {
                e1.printStackTrace();
            }
                return null;
        }
        //如果没带注解,则返回错误视图
            return "common/error";
    }

    @ExceptionHandler(UnauthorizedException.class)
    public Object handleException1(RuntimeException e, HandlerMethod method, HttpServletResponse response){
        e.printStackTrace();//打印异常信息方便开发找bug

        //判断是否带有ResponseBody注解
        if (method.hasMethodAnnotation(ResponseBody.class)){
            //带注解的话,返回jsonResult
            JsonResult jsonResult = new JsonResult(false,"你没有权限操作!");
            //使用response对象输出数据(json字符串)
            response.setContentType("application/json;charset=utf-8");
            try {
                response.getWriter().print(JSON.toJSONString(jsonResult));
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            return null;
        }else {
            //如果没带注解,则返回错误视图
            return "common/nopermission";

        }
    }

/*    @ExceptionHandler(AuthorizationException.class)
    public Object handleException2(AuthorizationException e, HandlerMethod method, HttpServletResponse response){
        e.printStackTrace();//打印异常信息方便开发找bug

        //判断是否带有ResponseBody注解
        if (method.hasMethodAnnotation(ResponseBody.class)){
            //带注解的话,返回jsonResult
            JsonResult jsonResult = new JsonResult(false,"你没有权限操作!");
            //使用response对象输出数据(json字符串)
            response.setContentType("application/json;charset=utf-8");
            try {
                response.getWriter().print(JSON.toJSONString(jsonResult));
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            return null;
        }else {
            //如果没带注解,则返回错误视图
            return "common/nopermission";

        }
    }*/

}
